package com.zzyl.service.impl;

import ch.qos.logback.core.joran.util.beans.BeanUtil;
import com.zzyl.dto.BedDto;
import com.zzyl.entity.Bed;
import com.zzyl.mapper.BedMapper;
import com.zzyl.service.BedService;
import com.zzyl.vo.BedVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class BedServiceImpl implements BedService {

    @Autowired
    private BedMapper bedMapper;

    @Override
    public List<BedVo> getBedsByRoomId(Long roomId) {
        return bedMapper.getBedsByRoomId(roomId);
    }

    @Override
    public int addBed(BedDto dto) {
        Bed bed = new Bed();
        BeanUtils.copyProperties(dto, bed);

        bed.setBedStatus(0);

        LocalDateTime now = LocalDateTime.now();
        bed.setCreateTime(now);
        bed.setUpdateTime(now);

        bed.setCreateBy(1L);
        bed.setUpdateBy(1L);

        return bedMapper.insertBed(bed);

    }

    @Override
    public BedVo getBedById(Long id) {
        Bed bedById = bedMapper.getBedById(id);
        BedVo bedVo = new BedVo();
        BeanUtils.copyProperties(bedById, bedVo);
        return bedVo;
    }

    @Override
    public int updateById(BedDto dto) {
        if (dto == null) {
            return 0;
        }
        Bed bed = new Bed();
        BeanUtils.copyProperties(dto,bed);
        bed.setUpdateTime(LocalDateTime.now());
        return bedMapper.updateById(bed);
    }

    @Override
    public void deleteById(Long id) {
        bedMapper.deleteById(id);
    }


}
